﻿using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Redis读写分离.Common;
using Redis读写分离.Filter;

namespace Redis读写分离
{
    public class Program
    {
        static void Main(string[] args)
        {

            IConfigurationRoot configuration = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json")
                .Build();

            //RedisMasterSlaveConfig? masterSlaveConfig = configuration.GetSection("RedisConnections").Get<RedisMasterSlaveConfig>();

            //ServiceProvider serviceProvider = new ServiceCollection()
            //    .AddOptions()
            //    .AddLogging(builder => builder.AddConsole())
            //    .BuildServiceProvider();

            /*
            var filter = new CuckooFilter(1024);

            filter.Add("apple");
            filter.Add("banana");
            filter.Add("cherry");

            Console.WriteLine(filter.Contains("apple"));   // True
            Console.WriteLine(filter.Contains("banana"));  // True
            Console.WriteLine(filter.Contains("grape"));   // False

            filter.Delete("banana");
            Console.WriteLine(filter.Contains("banana"));  // False
            */

            //var filter2 = new BloomFilter(10000, 0.001);



            //ThreadLocal<string> threadLocal = new ThreadLocal<string>();

            //Task<string> task1 = Task.Run(async () =>
            //{
            //    threadLocal.Value = "task1";
            //    await Task.Delay(1000);
            //    return threadLocal.Value;
            //});

            //Task<string> task2 = Task.Run(async () =>
            //{
            //    threadLocal.Value = "task2";
            //    return threadLocal.Value;
            //});

            //Console.WriteLine(task2.Result);    // 输出为 task2
            //Console.WriteLine(task1.Result);    // 输出为 "" 原因 await 之后线程就已经切换了，此时拿到的不是之前的上下文关联的数据

            //Console.ReadLine();




            //List<string> apiList = new List<string>()
            //{
            //    "api/User/Login",
            //    "api/User/Register",
            //    "api/User/GetUserInfo",
            //    "api/User/UpdateUser",
            //    "api/Order/DoOrder",
            //    "api/Order/Payment",
            //    "api/Order/Cancel"
            //};

            //List<string> apiRegList = new List<string>()
            //{
            //    "api/User/*",   // 通配符风格 `*` 表示匹配任意字符，但不含路径分隔符 `/`  ❌ `api/User/me/profile` （`*` 只匹配一级）
            //    "api/Order/*"
            //};

            //string apiName = "api/User/Login2";

            //// 1. 字符匹配
            //if (apiList.Contains(apiName))
            //{
            //    Console.WriteLine("ApiList 匹配成功");
            //    return;
            //}
            //// 2. 正则匹配
            //foreach (var pattern in apiRegList)
            //{
            //    Regex regex = new Regex(pattern);
            //    Match match = regex.Match(apiName);
            //    if (match.Success)
            //    {
            //        Console.WriteLine("ApiRegList 匹配成功");
            //    }
            //}



            //string? pubKey = configuration["RSA:pubKey"];
            //string? prKey = configuration["RSA:prKey"];

            //byte[] pubBytes = Convert.FromBase64String(pubKey);
            //byte[] prBytes = Convert.FromBase64String(prKey);

            //string rawText = "WebAPI";
            //using (var rsa = RSA.Create())
            //{
            //    rsa.ImportRSAPublicKey(pubBytes,out _);
            //    byte[] encryptedBytes = rsa.Encrypt(Encoding.UTF8.GetBytes(rawText),RSAEncryptionPadding.Pkcs1);

            //    rsa.ImportRSAPrivateKey(prBytes, out _);
            //    byte[] rawBytes = rsa.Decrypt(encryptedBytes,RSAEncryptionPadding.Pkcs1);
            //    string rawStr = Encoding.UTF8.GetString(rawBytes);
            //    Console.WriteLine(rawStr);
            //}





        }
    }
}
